/*
 * @Date: 2025-06-13 10:50:10
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-08-29 15:46:55
 * @FilePath: \corpus\src\pages\Home\DeptPermision\index.tsx
 */
import { getDeptUserList } from '@/services/ant-design-pro/dept';
import { PlusOutlined } from '@ant-design/icons';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { Button, Popconfirm } from 'antd';
import type React from 'react';
import { useState } from 'react';
import AddDeptUser from '../AddDeptUser';
import hooks from '../hooks/useHooks';
import PermisionMenu from '../PermisionMenu';

const DeptPermision: React.FC = () => {
  const [checked, setChecked] = useState([]);
  const { actionRef, setPermisionVisible, deleteDept, setAddUserVisible } = hooks.useContainer();
  const [account, setAccount] = useState<number>();
  const columns: ProColumns<any>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
      fixed: 'left',
      title: '序号',
    },

    {
      title: '单位名称',
      dataIndex: 'deptName',
      ellipsis: true,
      fixed: 'left',
    },

    {
      title: '姓名',
      dataIndex: 'userName',
      width: 146,
      ellipsis: true,
    },

    {
      title: '职务',
      dataIndex: 'e',
      width: 200,
      ellipsis: true,
      hideInSearch: true,
    },

    {
      title: '知识库权限',
      dataIndex: 'knowledges',
      ellipsis: true,
      width: 200,
      hideInSearch: true,
      render: (_, record) => {
        return <>{record.knowledges?.map((item) => item.name)?.join(',') ?? '-'}</>;
      },
    },

    {
      title: '操作',
      width: 100,
      dataIndex: 'option',
      hideInSearch: true,

      fixed: 'right',

      render: (_, record: any) => [
        <Popconfirm
          key={1}
          title="修改权限"
          description={`确认修改当前权限？`}
          onConfirm={() => {
            console.log(record);
            setChecked(record.knowledges.map((item: any) => item.id));
            setAccount(record.account);
            setPermisionVisible(true);
          }}
          onCancel={() => {}}
          okText="确认"
          cancelText="取消"
        >
          <a style={{ marginRight: '10.0px' }}>权限管理</a>
        </Popconfirm>,

        <Popconfirm
          key={2}
          title="删除当前用户"
          description={`确认删除当前用户？`}
          onConfirm={() => {
            deleteDept.run(record.account);
          }}
          onCancel={() => {}}
          okText="确认"
          cancelText="取消"
        >
          <a style={{ marginRight: '10.0px' }}>删除</a>
        </Popconfirm>,
      ],
    },
  ];
  return (
    <div style={{ padding: '10.0px' }}>
      <ProTable<API.RuleListItem, API.PageParams>
        rowKey="key"
        cardBordered
        actionRef={actionRef}
        toolBarRender={() => [
          <Button
            key="primary"
            onClick={() => {
              setAddUserVisible(true);
            }}
            type="primary"
          >
            <PlusOutlined />
            新建
          </Button>,
        ]}
        request={async (
          // 第一个参数 params 查询表单和 params 参数的结合
          // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
          params: any,
        ) => {
          // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
          // 如果需要转化参数可以在这里进行修改

          const msg = await getDeptUserList({
            ...params,
          });
          return {
            data: msg.data,
            // success 请返回 true，
            // 不然 table 会停止解析数据，即使有数据
            success: true,
            // 不传会使用 data 的长度，如果是分页一定要传
            total: msg.total,
          };
        }}
        search={{
          labelWidth: 'auto',
        }}
        pagination={{
          showSizeChanger: true,
          defaultPageSize: 10,
        }}
        columns={columns}
      />
      <AddDeptUser />
      <PermisionMenu check={checked} account={account} />
    </div>
  );
};

export default () => {
  return (
    <hooks.Provider>
      <DeptPermision />
    </hooks.Provider>
  );
};
